home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 8841 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  4.6 KB

  1. Path: newshost.lanl.gov!tanmoy
  2. From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Memory. How is it organised?
  5. Date: 04 Mar 1996 15:50:10 GMT
  6. Organization: Los Alamos National Laboratory
  7. Message-ID: <TANMOY.96Mar4085010@qcd.lanl.gov>
  8. References: <4hf5gs$1f7@news.mistral.co.uk>
  9. NNTP-Posting-Host: qcd.lanl.gov
  10. Mime-Version: 1.0
  11. Content-Type: text
  12. In-reply-to: mikebarnard@mistral.co.uk's message of Mon, 04 Mar 1996 14:12:18 GMT
  13.  
  14. In article <4hf5gs$1f7@news.mistral.co.uk>
  15. mikebarnard@mistral.co.uk (Mike Barnard) writes:
  16.  
  17. <snip>
  18. MB: I want to find out as much as possible about the stack and
  19. MB: the heap. I know some of this is machine specific, but not
  20. MB: all of it.
  21.  
  22. You know incorrectly: all of it is machine or architecture specific. 
  23.  
  24. MB: 
  25. MB: Suppose I did this?
  26. MB: 
  27. MB: struct tag
  28. MB:     {
  29. MB:     // Variables taking 100 bytes, for an example
  30. MB:     };
  31. MB: 
  32. MB: struct tag arrayname[50];
  33. MB: 
  34. MB: What happens? Is 5000 bytes of memory reserved for the
  35. MB: contents of the array? Where? Stack, heap or somewhere else?
  36. MB: Or does the compiler just make a "note" that it will be
  37. MB: needed then allocate some memory when the array(s) are
  38. MB: initialised. Again, where? (What is the definition of stack
  39. MB: or heap?)
  40.  
  41. All of those answers may be correct. Even on machines on which stack
  42. and heap make sense, it depends whether the declaration appears inside
  43. a function or at file scope. If it occurs at file scope, and no later
  44. initialized declaration is seen, a region of 50 * sizeof(struct tag)
  45. is reserved for arrayname when the end of translation unit is
  46. seen. Note that sizeof(struct tag) >= 100 bytes, the compiler can
  47. leave space between your variables or at the end of the struct for
  48. performance or other reasons.
  49.  
  50. If it is at file scope and a later initialized declaration is seen, it
  51. is that declaration that defines arrayname and guarantees that storage
  52. is reserved. In both these cases, storage is most likely to be
  53. generated `on heap', if that concept makes sense. It is actually
  54. better to think of this as some global region: and reserve heap for
  55. the dynamically allocatable space: it makes no difference.
  56.  
  57. If it appears inside a function, it is allocated `on stack', again, if
  58. that makes sense. It may even not be allocated if the compiler can
  59. figure out it is not needed. If the compiler can determine that a
  60. function cannot be called recursively (directly or indirectly), and it
  61. has large stack variable, the compiler may move it to heap instead.
  62.  
  63. MB: 
  64. MB: Should I malloc the array? How? That is to say; malloc as a
  65. MB: part of the array definition, or seperately looping through
  66. MB: each member of the array and mallocing it after it's been
  67. MB: defined?
  68. MB: 
  69.  
  70. Instead of asking vague questions, I would advise you to learn C
  71. systematically. If you have any real problem when you are learning,
  72. please post some example code so that we know what we are talking
  73. about. 
  74.  
  75. MB: Someone said on an irc channel about C++ that malloc is
  76. MB: obsolete. "Use the new command" he said. Then logged off!!!
  77. MB: I presume there's something in C++ that relates to this, but
  78. MB: I don't feel ready for OO yet. I want to learn to walk, not
  79. MB: run.
  80.  
  81. Decide whether you want to learn C++ or C. The two are different
  82. languages. OO is a style of programming: not of coding. Some languages
  83. may make OO easier than others, in fact some languages may have been
  84. designed with OO programming in mind, but all these languages we are
  85. talking about are turing equivalent, and all these languages are
  86. powerful enough to express OO constructs.
  87.  
  88. If you decide to learn C++, post to comp.lang.c++ instead.
  89.  
  90. In any case, since you bring it up, the issue is type safety. malloc
  91. gives you a region of memory of a specified size to do what you want
  92. with it. Raw memory is untyped, and as far as C is concerned the only
  93. problem is that you can manage to treat it as different types at
  94. different moments and tie yourself into knots. In C++, as objects are
  95. more complicated things (because of native support for OO), there are
  96. more possiblitites for problems arising from misuse of malloc.
  97.  
  98. new is not a keyword in C, so you do not have the option of replacing
  99. malloc with new.
  100.  
  101. Cheers
  102. Tanmoy
  103. --
  104. tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
  105. Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
  106. Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
  107. <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
  108. internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
  109. fax: 1 (505) 665 3003   voice: 1 (505) 665 4733    [ Home: 1 (505) 662 5596 ]
  110.